package com.sch.wanandroid.ui.my.rank

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.sch.wanandroid.entity.CoinRecordBean
import com.sch.wanandroid.entity.RankBean
import com.sch.wanandroid.network.NetWorkManager
import kotlinx.coroutines.launch
import java.lang.Exception

/**
 * Created by Sch.
 * Date: 2020/12/15
 * description:
 */
class RankViewModel : ViewModel() {
    private val _rankList = MutableLiveData<List<RankBean>>()
    val rankLiveData: LiveData<List<RankBean>> = _rankList

    private val _errorMsg = MutableLiveData<String?>()
    val errorMsg: LiveData<String?> = _errorMsg
    fun getRankData(pageNum: Int) {
        viewModelScope.launch {
            try {
                val result = NetWorkManager.apiService.getRankListData(pageNum)
                if (result.errorCode != 0) {
                    _errorMsg.value = result.errorMsg
                } else {
                    _rankList.value = result.data.datas
                }
            } catch (e: Exception) {
                _errorMsg.value = e.toString()
            }
        }
    }


}